package com.story.story_serve.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.story.story_pojo.userinfo;
import com.story.story_serve.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * ClassName:  UserService
 * description:
 *
 * @ author:郭海龙
 * @ Create：2024/4/30 15:16
 * @ Version:1.0
 */
@Service   //控制反转    自动生成实现类对象，并交给ioc容器管理
public class UserService {
    @Autowired     //依赖注入
    UserMapper userMapper;

    //自动将返回结果封装成userinfo对象，并放在List集合中
    public List<userinfo> getAllUsers() {
        return userMapper.selectList(null);
    }

    //删除
    public int delete(userinfo user)
    {
        return userMapper.delById(user);
    }

    //登录
    public String login(userinfo userinfo, HttpSession session) {
        //定义一个查询器
        QueryWrapper<userinfo> qw = new QueryWrapper<>(userinfo);
        userinfo userinfo1 = userMapper.selectOne(qw);
        if(userinfo1 != null){
            //登录成功将用户信息存放在session对象中
            session.setAttribute("user",userinfo1);
            return "登录成功";
        }else {
            return "用户名或密码错误";
        }

    }

    // 注册
    public String register(userinfo userinfo) {
        return userMapper.insert(userinfo) > 0 ? "注册成功" : "注册失败";
    }

    // 修改个人数据
    public String updateMyInfo(userinfo userinfo, HttpSession session) {

        userinfo user = (userinfo) session.getAttribute("user");

        user.setNikename(userinfo.getNikename());
        user.setUserpassword(userinfo.getUserpassword());
        user.setUserimg(userinfo.getUserimg());

        int num = userMapper.updateById(user);
        if (num == 0){
            return "修改失败";
        }else {
            return "修改成功";
        }

    }
}
